package com.linkage.jz.bankruptcy.adapter;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProManagerSelListViewAdapter extends BGARecyclerViewAdapter<ManagerUserInfo> {
    private Activity mActivity;
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    public ProManagerSelListViewAdapter(RecyclerView recyclerView, Activity activity) {
        super(recyclerView, R.layout.item_sel_managerlist);
        mActivity = activity;
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.iv_manageuser_add);

        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ManagerUserInfo userInfo) {
//        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "111")
//                .setText(R.id.tv_item_bgaswipe_detail,"22");

        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        if ( userInfo.getCurType()== 1) {
            viewHolderHelper.setVisibility(R.id.test, View.GONE);
            viewHolderHelper.setVisibility(R.id.test2,View.VISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.VISIBLE);
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getName());
//            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getUser().getLoginname());
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, userInfo.getSubsidiaryOrgan());
            ImageView imageview = viewHolderHelper.getView(R.id.iv_item_bgaswipe_avatar1);
            Glide.with(mActivity).load(userInfo.getUser().getAvatar())
                    .bitmapTransform(new CropCircleTransformation(mActivity))
                    .placeholder(R.drawable.logo)
                    .into(imageview);

            String id ="";
            if(BankruptcyApp.getInstance().selManger!=null )
            {
                id = BankruptcyApp.getInstance().selManger.getId();
            }

            if(id.equalsIgnoreCase(userInfo.getId()))
            {
                viewHolderHelper.setImageResource(R.id.iv_item_bgaswipe_sel,R.drawable.complete_green);
            }else{
                viewHolderHelper.setImageResource(R.id.iv_item_bgaswipe_sel,R.drawable.complete_full);
            }
        }else{
            viewHolderHelper.setVisibility(R.id.test2, View.GONE);
            viewHolderHelper.setVisibility(R.id.test,View.VISIBLE);

            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
            viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, userInfo.getGroup().getName()+"（"+userInfo.getGroupnum()+"）");

        }

        viewHolderHelper.setVisibility(R.id.iv_manageuser_add,View.GONE);
        viewHolderHelper.setVisibility(R.id.iv_phone,View.GONE);
        viewHolderHelper.setVisibility(R.id.iv_email,View.GONE);


//            swipeItemLayout.setSwipeAble(false);
//        } else {
            swipeItemLayout.setSwipeAble(false);
//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}